package org.knime.core.data.uri;

import java.net.URI;

import org.apache.commons.io.FilenameUtils;
import org.knime.core.data.DataCell;
import org.knime.core.data.DataCellFactory.FromComplexString;
import org.knime.core.data.DataCellFactory.FromSimpleString;
import org.knime.core.data.DataType;
import org.knime.core.data.convert.DataCellFactoryMethod;

/**
 * A factory for creating {@link URIDataCell}s.
 *
 * @author Thorsten Meinl, KNIME AG, Zurich, Switzerland
 * @since 3.0
 */
public class UriCellFactory implements FromSimpleString, FromComplexString {
    /**
     * The data type generated by this factory.
     */
    public static final DataType TYPE = URIDataCell.TYPE;

    /**
     * Creates a new URI data cell.
     *
     * @param s a string with a URI
     * @return a new data cell
     * @throws IllegalArgumentException if the string does not denote a valid URI
     */
    @SuppressWarnings("deprecation")
    public static DataCell create(final String s) {
        final URI uri = URI.create(s);

        var ext = "";
        if (uri.getPath() != null && !uri.getPath().isBlank()) {
            ext = FilenameUtils.getExtension(uri.getPath());
        }
        return new URIDataCell(new URIContent(uri, ext));
    }

    @DataCellFactoryMethod(name = "String")
    @Override
    public DataCell createCell(final String input) {
        return create(input);
    }

    @Override
    public DataType getDataType() {
        return TYPE;
    }
}
